<template>
  <view class="container">
    <view v-for="(item, index) in evaluations" :key="index" class="evaluation-item">
      <view class="top-row">
        <text class="name">{{ formatName(item.name) }}</text>
        <text class="time">评价时间：{{ formatTime(item.reviewTime) }}</text>
        <text class="order-number" @click="goToOrderDetail(item.orderId)">{{ item.orderId }}</text>
      </view>
      <view class="rating-row">
        <text class="rating-text">综合评分：{{ item.rating }}分</text>
        <uni-rate
          :value="item.rating"
          :max="5"
          :size="18"
          color="#bbb"
          active-color="#FFC107"
          :readonly="true"
          :is-fill="true"
        />
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import uniRate from '@/uni_modules/uni-rate/components/uni-rate/uni-rate.vue';
import request from '../../utils/request';
import dayjs from 'dayjs'; // 引入 dayjs

// 评价数据
const evaluations = ref([]);

// 格式化姓名，只显示第一个字，后面用星号代替
const formatName = (name) => {
  if (name && name.length > 0) {
    return name[0] + '*'.repeat(name.length - 1);
  }
  return '';
};

// 使用 dayjs 格式化时间
const formatTime = (isoTime) => {
  return dayjs(isoTime).format('YYYY-MM-DD HH:mm');
};

// 跳转到订单详情页
const goToOrderDetail = (orderId) => {
  // 根据 orderId 找到对应的评价数据
  const item = evaluations.value.find((evaluation) => evaluation.orderId === orderId);
  if (!item) {
    console.error('未找到对应的订单信息');
    return;
  }

  // 将订单信息和评价信息转换为 JSON 字符串
  const orderData = JSON.stringify(item.orderData || {});
  const reviewData = JSON.stringify(item.reviewData || {});

  // 跳转并传递数据
  uni.navigateTo({
    url: `/pages/evaluation/orderDetail/orderDetail?orderData=${encodeURIComponent(orderData)}&reviewData=${encodeURIComponent(reviewData)}`,
  });
};

// 获取评价数据
const fetchMerchantData = () => {
  const storeId = uni.getStorageSync('storeId');
  request({
    url: '/mstore/store/list2',
    method: 'GET',
    params: {
      storeId: storeId, // 将 storeId 作为参数传递
    },
  })
    .then((res) => {
      if (res.code === 200) {
        // 提取评价数据
        const evaluationsData = res.data.orderList.map((order) => ({
          name: res.data.merchant.principal, // 负责人姓名
          reviewTime: order.serviceReview.reviewTime, // 评价时间
          orderId: order.orderId, // 订单号
          rating: order.serviceReview.rating, // 评分
          content: order.serviceReview.content, // 评价内容
          orderData: order, // 订单信息
          reviewData: order.serviceReview, // 评价信息
        }));
        // 更新评价数据
        evaluations.value = evaluationsData;
      } else {
        console.error('请求失败:', res.msg);
      }
    })
    .catch((error) => {
      console.error('请求异常:', error);
    });
};

// 页面加载时调用接口获取数据
onMounted(() => {
  fetchMerchantData();
});
</script>

<style scoped>
.container {
  padding: 20px;
}

.evaluation-item {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.top-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.name {
  font-size: 16px;
  font-weight: bold;
}

.time {
  font-size: 12px;
  color: #999;
}

.order-number {
  font-size: 12px;
  color: #007AFF;
  text-decoration: underline;
}

.rating-row {
  display: flex;
  align-items: center;
}

.rating-text {
  font-size: 14px;
  margin-right: 10px;
}
</style>